package com.priserve.retirement.dao;

import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.service.ServiceRegistryBuilder;

public class RetirementHibernateUtil {

	public RetirementHibernateUtil() {
		
	}

		private static SessionFactory sessionFactory; 
		private static ServiceRegistry serviceRegistry;
		
		   
		public void createSessionFactory(){ 
		        
		try { 
		            
			Configuration configuration = new Configuration();
			configuration.configure();
			serviceRegistry = new ServiceRegistryBuilder().applySettings(configuration.getProperties()).buildServiceRegistry();
			sessionFactory = configuration.buildSessionFactory(serviceRegistry);		        
		} 
		catch (Throwable ex) { 
		            
		System.err.println("*********----**************Initial SessionFactory creation failed." + ex.getMessage());
		ex.printStackTrace();
		            
		throw new ExceptionInInitializerError(ex); 
		        
		} 
		    
		} 
		   
		public  SessionFactory getSessionFactory() { 
		        
		return sessionFactory; 
		    
		}
		
		public  Session getSession(){
			createSessionFactory();	
		Session session=	sessionFactory.openSession();
		
		return session;
		
	}
		
		public void saveOrUpdate(Object obj) {
			
			Session session1=getSession();
			session1.saveOrUpdate(obj);
			session1.close();
			
		}
		
		
		public Criteria createCriteria(Class className){
			
			Session session=getSession();
			Criteria criteria=session.createCriteria(className);
			
			return criteria;
		}
		
		
		
		/*public Object selectByCriteria(Criteria criteria){
			
			return null;
			
		}*/
			

		
		
		

}
